package com.hujing;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterDemo {

    public static void main(String[] args) {
        //创建DateTimeFormatter对象
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //获取当前日期时间
        LocalDateTime now = LocalDateTime.now();
        //格式化当前日期时间
        System.out.println(formatter.format(now));

        //方向格式化
        String s = now.format(formatter);
        System.out.println(s);

        //解析日期时间字符串
        LocalDateTime localDateTime = LocalDateTime.parse("2021年08-01 10:15:30", DateTimeFormatter.ofPattern("yyyy年MM-dd HH:mm:ss"));
        System.out.println(localDateTime);
    }
}
